//
//  ShareWiFiView.m
//  JmzGo
//
//  Created by apple  on 2017/9/19.
//  Copyright © 2017年 金拇指. All rights reserved.
//

#import "ShareWiFiView.h"

#define ViewH 220

@interface ShareWiFiView()<UITextFieldDelegate>{
    UIView *_contentView;
    UILabel *titleLabel;
    UITextField *passwordField;
    UILabel *sharePlainLabel;   //分享计划
}

@end

@implementation ShareWiFiView

- (id)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        [self initContent];
    }
    
    return self;
}

- (void)initContent
{
    self.frame = CGRectMake(0, 0, ScreenW, ScreenH);
    
    //alpha 0.0  白色   alpha 1 ：黑色   alpha 0～1 ：遮罩颜色，逐渐
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
    self.userInteractionEnabled = YES;
    [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMissView)]];
    
    if (_contentView == nil)
    {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(20, (ScreenH - ViewH)/5, ScreenW - 40, ViewH)];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.cornerRadius = 5;
        [self addSubview:_contentView];
    }
    
    titleLabel = [UILabel new]; //initWithFrame:CGRectMake(10, 0, ScreenW, 30)];
//    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor hexColor:@"2D8DEF"];
    titleLabel.text = @"分享：";
    [_contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_contentView.mas_top).offset(15);
        make.leftMargin.offset(15);
        make.height.offset(25);
    }];
    
    passwordField = [UITextField new];
    passwordField.delegate = self;
    passwordField.returnKeyType= UIReturnKeyDone;
    passwordField.placeholder = @"输入热点密码";
    [_contentView addSubview:passwordField];
    [passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(10);
        make.leftMargin.offset(5);
        make.rightMargin.offset(-5);
        make.width.offset(ScreenW - 10);
        make.height.offset(40);
    }];
    
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor hexColor:@"2D8DEF"];
    [_contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passwordField.mas_bottom).offset(2);
        make.left.equalTo(passwordField.mas_left);
        make.right.equalTo(passwordField.mas_right);
        make.height.offset(1);
    }];
    
    sharePlainLabel = [UILabel new];
    sharePlainLabel.numberOfLines = 0;
    [sharePlainLabel sizeToFit];
    sharePlainLabel.textColor = [UIColor darkGrayColor];
//    sharePlainLabel.text = @"分享热点/已阅读并同意《热点互助分享计划》";
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:@"分享热点/已阅读并同意《热点互助分享计划》"];
    NSRange fontRange = NSMakeRange([[noteStr string] rangeOfString:@"分享热点"].location, [[noteStr string] rangeOfString:@"分享热点"].length);
    [noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:fontRange];
    NSRange colorRange = NSMakeRange([[noteStr string] rangeOfString:@"《热点互助分享计划》"].location, [[noteStr string] rangeOfString:@"《热点互助分享计划》"].length);
    //需要设置的位置
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor hexColor:@"4682B4"] range:colorRange];
    
    // 下划线
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    [noteStr addAttributes:attribtDic range:colorRange];
    
    [sharePlainLabel setAttributedText:noteStr];
    sharePlainLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
    
    [sharePlainLabel addGestureRecognizer:labelTapGestureRecognizer];
    [_contentView addSubview:sharePlainLabel];
    [sharePlainLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line.mas_bottom).offset(15);
        make.leftMargin.offset(3);
        make.rightMargin.offset(-3);
        make.width.equalTo(_contentView);
    }];
    
    UIView *bottomView = [UIView new];
    [_contentView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(sharePlainLabel.mas_bottom).offset(20);
        make.width.offset(ScreenW);
        make.height.offset(50);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    cancelBtn.frame = CGRectMake(0, 0, screen_width/2, 40);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    [cancelBtn setBackgroundColor:[UIColor hexColor:@"ea5514"]];
    cancelBtn.tag = 1001;
    [cancelBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomView.mas_top);
        make.width.offset(ScreenW/2);
        make.height.offset(50);
    }];
    
    UIButton *submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    submitBtn.frame = CGRectMake(screen_width/2, 0, screen_width/2, 40);
    [submitBtn setTitle:@"确定" forState:UIControlStateNormal];
    [submitBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//    [submitBtn setBackgroundColor:[UIColor hexColor:@"ea5514"]];
    submitBtn.tag = 1002;
    [submitBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:submitBtn];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(bottomView.mas_top);
        make.left.equalTo(cancelBtn.mas_right);
        make.width.offset(ScreenW/2);
        make.height.offset(50);
    }];
    
}

//展示从底部向上弹出的UIView（包含遮罩）
- (void)showInView:(UIView *)view withSSID:(NSString *)ssid
{
    if (!view)
    {
        return;
    }
    titleLabel.text = [NSString stringWithFormat:@"分享：%@",ssid];
    [view addSubview:self];
    [view addSubview:_contentView];
    [_contentView setFrame:CGRectMake(20, (ScreenH - ViewH)/5, ScreenW - 40, ViewH)];
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.alpha = 1.0;
        
        [_contentView setFrame:CGRectMake(20, (ScreenH - ViewH)/5, ScreenW - 40, ViewH)];
        
    } completion:nil];
    
}

//移除从上向底部弹下去的UIView（包含遮罩）
- (void)disMissView
{
    [_contentView setFrame:CGRectMake(20, (ScreenH - ViewH)/5, ScreenW - 40, ViewH)];
    [UIView animateWithDuration:0.3f
                     animations:^{
                         
                         self.alpha = 0.0;
                         
                         [_contentView setFrame:CGRectMake(0, ScreenH, ScreenW, ViewH)];
                     }
                     completion:^(BOOL finished){
                         
                         [self removeFromSuperview];
                         [_contentView removeFromSuperview];
                         
                     }];
    
}

- (void)buttonAction:(UIButton *)sender
{
    if (self.shareWiFiViewClick) {
        self.shareWiFiViewClick(sender.tag, passwordField.text);
    }
    
    [self disMissView];
}

-(void)labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    
    UILabel *label=(UILabel*)recognizer.view;
    
    NSLog(@"%@被点击了",label.text);
    if (self.shareWiFiViewClick) {
        self.shareWiFiViewClick(1003, nil);
    }
    
    [self disMissView];
   
}

@end
